package com.tma.android.gpm.data.database.handler;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.android.gm.api.model.Song;
import com.tma.android.gpm.data.database.util.DatabaseTable;
import com.tma.android.gpm.data.database.util.DatabaseTable.TableSongs;

public class SongsTableHandler extends TableAdapter implements TableHandler<Song>{

	
	
	public SongsTableHandler(Context context) {
		super(context);
	}

	@Override
	public void addItem(Song Item) {
		this.open();
		try
		{
			ContentValues values = new ContentValues();
			values.put(TableSongs.mAlbum, Item.getAlbum());
			values.put(TableSongs.mAlbumArtist, Item.getAlbumArtist());
			values.put(TableSongs.mAlbumArtistNorm, Item.getAlbumArtistNorm());
			values.put(TableSongs.mAlbumArtUrl, Item.getAlbumArtUrl());
			values.put(TableSongs.mArtist, Item.getArtist());
			values.put(TableSongs.mArtistNorm, Item.getArtistNorm());
			values.put(TableSongs.mBeatsPerMinute, Item.getBeatsPerMinute());
			values.put(TableSongs.mComment, Item.getComment());
			values.put(TableSongs.mComposer, Item.getComposer());
			values.put(TableSongs.mCreationDate, Item.getCreationDate());
			values.put(TableSongs.mDisc, Item.getDisc());
			values.put(TableSongs.mDurationMillis, Item.getDurationMillis());
			values.put(TableSongs.mGenre, Item.getGenre());
			values.put(TableSongs.mId, Item.getId());
			values.put(TableSongs.mIsDeleted, Item.isDeleted());
			values.put(TableSongs.mLastPlayed, Item.getLastPlayed());
			values.put(TableSongs.mMatchedId, Item.getMatchedId());
			values.put(TableSongs.mPlayCount, Item.getPlayCount());
			values.put(TableSongs.mName, Item.getName());
			values.put(TableSongs.mRating, Item.getRating());
			values.put(TableSongs.mSubjectToCuration, Item.isSubjectToCuration());
			values.put(TableSongs.mTitle, Item.getTitle());
			values.put(TableSongs.mTitleNorm, Item.getTitleNorm());
			values.put(TableSongs.mTotalDiscs, Item.getTotalDiscs());
			values.put(TableSongs.mTotalTracks, Item.getTotalTracks());
			values.put(TableSongs.mTrack, Item.getTrack());
			values.put(TableSongs.mType, Item.getType());
			values.put(TableSongs.mUrl, Item.getUrl());
			values.put(TableSongs.mYear, Item.getYear());
//			values.put(TableSongs., value)
			this.mDb.insertOrThrow(DatabaseTable.TABLE_SONGS, null, values);
		}catch(Exception ex)
		{
			ex.printStackTrace();
		} finally
		{
			this.close();
		}
	}

	@Override
	public Song getItem(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Song> getAllItems() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getItemsCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateItem(Song Item) {
		this.open();
		int count = 0;
		try
		{
			ContentValues values = new ContentValues();
			values.put(TableSongs.mAlbum, Item.getAlbum());
			values.put(TableSongs.mAlbumArtist, Item.getAlbumArtist());
			values.put(TableSongs.mAlbumArtistNorm, Item.getAlbumArtistNorm());
			values.put(TableSongs.mAlbumArtUrl, Item.getAlbumArtUrl());
			values.put(TableSongs.mArtist, Item.getArtist());
			values.put(TableSongs.mArtistNorm, Item.getArtistNorm());
			values.put(TableSongs.mBeatsPerMinute, Item.getBeatsPerMinute());
			values.put(TableSongs.mComment, Item.getComment());
			values.put(TableSongs.mComposer, Item.getComposer());
			values.put(TableSongs.mCreationDate, Item.getCreationDate());
			values.put(TableSongs.mDisc, Item.getDisc());
			values.put(TableSongs.mDurationMillis, Item.getDurationMillis());
			values.put(TableSongs.mGenre, Item.getGenre());
			values.put(TableSongs.mId, Item.getId());
			values.put(TableSongs.mIsDeleted, Item.isDeleted());
			values.put(TableSongs.mLastPlayed, Item.getLastPlayed());
			values.put(TableSongs.mMatchedId, Item.getMatchedId());
			values.put(TableSongs.mPlayCount, Item.getPlayCount());
			values.put(TableSongs.mName, Item.getName());
			values.put(TableSongs.mRating, Item.getRating());
			values.put(TableSongs.mSubjectToCuration, Item.isSubjectToCuration());
			values.put(TableSongs.mTitle, Item.getTitle());
			values.put(TableSongs.mTitleNorm, Item.getTitleNorm());
			values.put(TableSongs.mTotalDiscs, Item.getTotalDiscs());
			values.put(TableSongs.mTotalTracks, Item.getTotalTracks());
			values.put(TableSongs.mTrack, Item.getTrack());
			values.put(TableSongs.mType, Item.getType());
			values.put(TableSongs.mUrl, Item.getUrl());
			values.put(TableSongs.mYear, Item.getYear());
//			values.put(TableSongs., value)
			count = this.mDb.update(DatabaseTable.TABLE_SONGS, values,TableSongs.mId + " = " + Item.getId(), null);
		}catch(Exception ex)
		{
			ex.printStackTrace();
		} finally
		{
			this.close();
		}
		return count;
	}

	@Override
	public void deleteItem(Song Item) {
		// TODO Auto-generated method stub
		this.open();
		int count = 0;
		try
		{
			
			count = this.mDb.delete(DatabaseTable.TABLE_SONGS, TableSongs.mId + " = " + Item.getId(), null);
		}catch(Exception ex)
		{
			ex.printStackTrace();
		} finally
		{
			this.close();
		}
	}

	@Override
	public boolean isExist(Song item) {
		// TODO Auto-generated method stub
		Cursor cursor = null;
		boolean exists = false;
		try {
			this.open();
			String sql = "SELECT 1 FROM " + DatabaseTable.TABLE_SONGS + " WHERE " + TableSongs.mId + " = " + item.getId();
			cursor = mDb.rawQuery(sql, null);
			exists = (cursor.getCount() > 0);
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return exists;
	}

	
	
	

}
